//
//  MainViewController.swift
//  SinaWeibo
//
//  Created by gaoliang on 15/11/9.
//  Copyright © 2015年 gaoliang. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    func composeDidClick() {
        let compose = composeViewController()
        let nav = UINavigationController(rootViewController: compose)
        presentViewController(nav, animated: true, completion: nil)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //由于 tabBar 是只读 不能够直接操作,如果要修改 可以使用 KVC
        
        // 添加子视图控制器
        addChildViewControllers()
        
        // 添加加号按钮
        let mainTabbar = MainTabBar()
        // KVC
        setValue(mainTabbar, forKey: "tabBar")
        // 添加点击事件
        mainTabbar.composeBtn.addTarget(self, action: "composeDidClick", forControlEvents: .TouchUpInside)
    }
    
    private func addChildViewControllers() {
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
         addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
         addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
         addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }

    // 重载
    private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
        
        // 实例化导航栏图片
        let nav = BaseNavViewController(rootViewController: vc)
        //        nav.title = "首页"
        vc.title = title
        // 设置图片及设置去掉图片的默认渲染
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        // 更改字体
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
        // 添加
        addChildViewController(nav)
    }

}
